package jzoffer;

/**
 * @author MaoLin Wang
 * @date 2020/10/8 9:23 上午
 */
public class _22链表倒数第k个节点 {
    public static ListNode getKthFromEnd(ListNode head, int k) {
        if (k<0 || head==null){
            return null;
        }
        ListNode fast=head;
        ListNode slow=head;

        int count=0;
        while (count< k-1){
            fast=fast.next;
            count++;
        }
        if (fast==null){
            return null;
        }
        while (fast.next!=null){
            fast=fast.next;
            slow=slow.next;
        }
        return slow;

    }

    public static void main(String[] args) {
        ListNode node=new ListNode(3);
        ListNode node1=new ListNode(4);
        ListNode node2=new ListNode(5);
        node.next=node1;
        node1.next=node2;
        System.out.println(getKthFromEnd(node,4));
    }
}
